namespace FireCol
{
    public partial class Form1 : Form
    {
        private RouterCompute m_router;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.pictureBox1.Paint += Form1_Paint;
        }

        private void Form1_Paint(object? sender, PaintEventArgs e)
        {
            e.Graphics.Clear(Color.White);

            if (m_router == null ) return;

            
            if (startNode != null)
            {
                /*var m = m_router.MaxCostsMapValue;
                for (var i = 0; i < m_router.Size; i++)
                {
                    for (var j = 0; j < m_router.Size; j++)
                    {
                        var r = (int)(m_router.CostsMap[i, j] * 255.0 / m);
                        var c = Color.FromArgb(r, r, r);
                        e.Graphics.FillRectangle(new SolidBrush(c), new RectangleF(i * 2, j * 2, 2, 2));
                    }
                }*/
                var m = m_router.MaxCellCost*0.8;
                if (m > 300) m = 300;
                for (var i = 0; i < m_router.Size; i++)
                {
                    for (var j = 0; j < m_router.Size; j++)
                    {
                        var r = (int)(m_router.CellCosts[i, j] * 255.0 / m);
                        if (r < 0)
                        {
                            r=0;
                        }
                        if (r>255)
                        {
                            r = 255;
                        }
                        var c = Color.FromArgb(255-r, 255 - r, 255 - r);
                        e.Graphics.FillRectangle(new SolidBrush(c), new RectangleF(i * 2, j * 2, 2, 2));
                    }
                }
                
                List<Point> outPoints = new List<Point>();
                startNode.Render(e.Graphics, outPoints);
            }

            e.Graphics.FillRectangle(Brushes.Blue, new RectangleF(m_router.StartPos.X * 2, m_router.StartPos.Y * 2, 2, 2));
            e.Graphics.FillRectangle(Brushes.Blue, new RectangleF(m_router.EndPos.X * 2, m_router.EndPos.Y * 2, 2, 2));

        }

        private PointPreNode startNode;
        private void button1_Click(object sender, EventArgs e)
        {
            this.pictureBox1.Refresh();

            m_router=new RouterCompute();
            m_router.InitCostsMap();

            m_router.AddFire(40, 50);
            m_router.AddWall(new Point(80, 140), new Point(180, 40));

            m_router.AddWall(new Point(00, 40), new Point(50, 40));

            m_router.FindRoute();

            startNode = new PointPreNode()
            {
                CurrentPos = m_router.EndPos
            };
            m_router.GoStep(startNode);

            this.pictureBox1.Refresh();
        }

        private void Timer1_Tick(object? sender, EventArgs e)
        {
            this.pictureBox1.Refresh();
        }
    }
}
